﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_LogLoginDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_LogLogin(T_LogLogin en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                conn.T_LogLogin.Add(en);
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        public static List<V_LogLogin> Get_LogLogin(out int count,string starttime, string endtime, string uname, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.V_LogLogin.Select(c => c);
                if (uname != null && uname != "")
                    query = query.Where(c => c.U_NAME.Contains(uname));
                if (starttime != null&& starttime != "") {
                    DateTime st = DateTime.Parse(starttime);
                    query = query.Where(c => c.LOG_DATE >= st);
                }
                if (endtime != null && endtime != "") {
                    DateTime et = DateTime.Parse(endtime);
                    query = query.Where(c => c.LOG_DATE <= et);
                }
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderByDescending(c=>c.LOG_DATE).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.OrderByDescending(c=>c.LOG_DATE).ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch(Exception ex)
            { return null; }
        }
    }
}
